import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', () => {
  // 用户ID，从localStorage中获取，如果没有则为null
  const userId = ref(localStorage.getItem('userId') || null)
  const userName = ref(localStorage.getItem('userName') || '')
  const userRole = ref(localStorage.getItem('userRole') || '')

  // 设置用户信息
  function setUserInfo(id, name, role) {
    userId.value = id
    userName.value = name
    userRole.value = role
    
    // 保存到localStorage
    localStorage.setItem('userId', id)
    localStorage.setItem('userName', name)
    localStorage.setItem('userRole', role)
  }

  // 清除用户信息
  function clearUserInfo() {
    userId.value = null
    userName.value = ''
    userRole.value = ''
    
    // 从localStorage中移除
    localStorage.removeItem('userId')
    localStorage.removeItem('userName')
    localStorage.removeItem('userRole')
  }

  return { userId, userName, userRole, setUserInfo, clearUserInfo }
}) 